package andev.schoolhelper.apputil;

import android.util.Log;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import andev.aandroidlib.activity.ANetActivity;
import andev.aandroidlib.util.AHttp;

public class SchoolUtil {
    public static String course_username = "", course_password = "";
    public static String xnd = "", xqd = "";
    private ANetActivity context;
    private ArrayList<JSONObject> courseJSONs;

    public SchoolUtil(ANetActivity context) {
        this.context = context;
    }

    public void loginToSchool(String username, String password) {
        SchoolUtil.course_username = username;
        SchoolUtil.course_password = password;

        Map<String, String> params = new HashMap<>();
        params.put("__VIEWSTATE", "dDwxOTI3MTM3Mjk0Ozs+VvRNi12t8HHaRxDPsMlaa62XxFQ=");
        params.put("__VIEWSTATEGENERATOR", "92719903");
        params.put("TextBox1", username);
        params.put("TextBox2", password);
        params.put("RadioButtonList1", "学生");
        params.put("Button1", "0");

        Map<String, String> headers = new HashMap<>();
        //headers.put("Host","jwxt.zwu.edu.cn");
        //headers.put("Origin","http://jwxt.zwu.edu.cn");
        headers.put("Referer", "http://jwxt.zwu.edu.cn/");

        AHttp aHttp = new AHttp(
                context,
                "http://jwxt.zwu.edu.cn/default2.aspx",
                //"http://angusphp.sinaapp.com/new_file.php",
                "POST",
                headers,
                params,
                new AHttp.HttpListener() {
                    @Override
                    public void OnSuccess(byte[] bytes) {
                        String body = null;
                        try {
                            body = new String(bytes, "gb2312");
                            if (!body.contains("欢迎您")) {
                                loginToSchoolFailed();
                            } else {
                                loginToSchoolSuccess();
                            }
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void OnFailure(int code, Map<String, List<String>> responseHeaders) {
                    }
                });
        aHttp.connect();
    }

    public void WatchCourse() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("__EVENTTARGET", "xqd");
        params.put("__EVENTARGUMENT", "0");
        String __VIEWSTATE = "";
        if (xqd.equals("1")) {
            __VIEWSTATE = "dDwxNzQzODM2MjIy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Q7PjtsPOavleaYpei3gy/mnajmtKrlsbE7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPDEuMDs+Pjs+Ozs+Oz4+Oz4+Oz4+Oz4+Oz6UHlBJJxGg162NP7XlDXMmDPbIcg==";
        }
        params.put("__VIEWSTATE", __VIEWSTATE);
        params.put("__VIEWSTATEGENERATOR", "55530A43");
        params.put("xnd", SchoolUtil.xnd);
        params.put("xqd", SchoolUtil.xqd);

        Map<String, String> headers = new HashMap<>();
        //headers.put("Host","jwxt.zwu.edu.cn");
        //headers.put("Origin","http://jwxt.zwu.edu.cn");
        headers.put("Referer", "http://jwxt.zwu.edu.cn/");
        AHttp aHttp = new AHttp(
                context,
                "http://jwxt.zwu.edu.cn/xskbcx.aspx?xh=" + course_username,
                "POST",
                headers,
                params,
                new AHttp.HttpListener() {
                    @Override
                    public void OnSuccess(byte[] bytes) {
                        try {
                            String whole = new String(bytes, "gb2312");
                            courseJSONs = new QueryCourseUtil().parseCourses(whole);
                            queryCourseCallback(courseJSONs);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void OnFailure(int code, Map<String, List<String>> responseHeaders) {
                        Log.i("", "code=" + code);
                    }
                });
        aHttp.connect();
    }

    public void queryCourseCallback(ArrayList<JSONObject> courseJSONs2) {
    }

    public void loginToSchoolFailed() {
    }

    public void loginToSchoolSuccess() {
    }

}